<?php
/*
对账文件下载
 */

require_once "init.php";
require_once "request/TransContent.php";
require_once "request/TransDataUtils.php";

echo "\r\n";

$key="86a8eec5ae958e9948b7450439cc57e2";
$params = new TransContent();
$params -> _set("method","QUERY_TRADE_FILE");
$params -> _set("version","1.0");
$params -> _set("format","json");
$params -> _set("merchantNo","814000473149");
$params -> _set("signType","RSA2");
$params -> _set("signContent","");
$params -> _set("sign","");

$body = new TransContent();
date_default_timezone_set("Asia/Shanghai");
$body -> _set("batchDate", "2020-07-15");//交易日期 yyyy-MM-dd
$body -> _set("billType", "TERMINAL_BILL");//文件类型


$bodyjson = TransDataUtils :: _array2Json($body -> _getValues());
$params -> _set("signContent",$bodyjson);


/**
* rsa 签名算法流程
* 1.先按固定格式拼接
* 2.RSA加签
*/
$signStr = "method=".$params->_get("method")."&version=".$params->_get("version")."&format=".$params->_get("format")."&merchantNo=".$params->_get("merchantNo")."&signType=".$params->_get("signType")."&signContent=".$params->_get("signContent")."&key=".$key;
echo "[请求]固定格式拼接后明文：".$signStr."\r\n";
$encrypted = $rsaUtil -> sign($signStr);
echo "[请求]RSA加签后内容：", $encrypted."\r\n";
$params -> _set("sign",$encrypted);

$array = $params -> _getValues();
echo "[客户端-发送] 内容：", TransDataUtils :: _array2Json($array)."\r\n";
$request_url = "https://test-api.huishouqian.com/api/acquiring";
$httpResult = $httpUtil -> post2($params -> _getValues(),$request_url);
echo "[服务端-响应] 内容：", $httpResult."\r\n";
$bodyArray = TransDataUtils :: _json2Array($httpResult);

/*
* rsa验签流程
 * 1.全报文除sign外按照固定格式拼接
 * 2.RSA验签
 */

$respResSign = $bodyArray['sign'];
if($bodyArray['success']){
$resSignStr = "result=".$bodyArray['result']."&success=true"."&key=".$key;
}
else{
$resSignStr = "errorCode=".$bodyArray['errorCode']."&errorMsg=".$bodyArray['errorMsg']."&success=false"."&key=".$key;
}
echo "[响应] 固定格式拼接后：".$resSignStr."\r\n";

$verifyResult = $rsaUtil -> verify($resSignStr,$respResSign);
//echo "[响应] RSA验签结果:：".$resMd5Str."\r\n";
if($verifyResult){
echo "验签通过";
}
else {
echo "验签失败";
}
?>